var selectedItems = fl.getDocumentDOM().library.getSelectedItems();
var allItems = fl.getDocumentDOM().library.items;

function swapItemLinkNames() {
	if(selectedItems.length == 2 && !isFolderIn(selectedItems) && selectedItems[0].linkageClassName != undefined && selectedItems[1].linkageClassName != undefined) {
		var item0 = selectedItems[0];
		var item1 = selectedItems[1];
		
		var name0 = item0.linkageClassName;
		var name1 = item1.linkageClassName;
		
		var i = 0;
		
		do {
			nameTmp = name0 + i;
			i++;
		} while(nameExists(nameTmp))
		
		item0.linkageClassName = nameTmp;
		item1.linkageClassName = name0;
		item0.linkageClassName = name1;
	}
}

function nameExists(nameToSearch) {
	var i = 0;
	var nbItems = allItems.length;
	for(; i < nbItems; i++) {
		if(allItems[i].linkageClassName) {
			if(allItems[i].linkageClassName.toLowerCase() == nameToSearch.toLowerCase()) {
				return true;
			}
		}
	}
	return false;
}

function isFolderIn(itemsList) {
	var i = 0;
	var nbItems = itemsList.length;
	for(; i < nbItems; i++) {
		if(itemsList[i].itemType == "folder") {
			return true;
		}
	}
	return false;
}

swapItemLinkNames();